package com.pz.cloud.framework.stdGroup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.cloud.framework.base.dao.CommonDao;
import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.base.service.BaseServiceImpl;
import com.pz.cloud.framework.business.society.entity.OrgSociety;
import com.pz.cloud.framework.business.society.entity.OrgSocietyUser;
import com.pz.cloud.framework.business.stdGroup.entity.OrgStdGroup;
import com.pz.cloud.framework.business.stdGroup.entity.OrgStdGroupUser;
import com.pz.cloud.framework.business.stdGroup.service.IOrgStdGroupService;
import com.pz.cloud.framework.business.stdGroup.service.IOrgStdGroupUserService;
import com.pz.cloud.framework.business.student.entity.Student;
import com.pz.cloud.framework.business.volunteerTeam.entity.VolunteerTeamUser;
import com.pz.cloud.framework.business.workflow.annotation.Workflow;
import com.pz.cloud.framework.model.AbstractSqlBuilder;
import com.pz.cloud.framework.model.SqlBuilder;
import com.pz.cloud.framework.stdGroup.callback.OrgStdGroupCallback;
import com.pz.cloud.framework.stdGroup.mapper.OrgStdGroupMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
public class OrgStdGroupServiceImpl extends BaseServiceImpl<OrgStdGroupMapper, OrgStdGroup> implements IOrgStdGroupService {

    @Autowired
    private CommonDao commonDao;
    @Autowired
    private IOrgStdGroupUserService orgStdGroupUserService;

    @Override
    @Workflow(callback = OrgStdGroupCallback.class)
    public boolean save(OrgStdGroup entity) {
        boolean flag ;
        if(StringUtils.isEmpty(entity.getId())){
            if (CollectionUtils.isNotEmpty(findByName(entity.getName()))) {
                throw new CustomRuntimeException("该学生组织已存在");
            }
            flag = super.save(entity);
        }else{
            flag = super.updateById(entity,true);
        }
        entity.getWorkflowModel().setHandlerId(entity.getInstructorId());

        OrgStdGroupUser stdGroupUser = orgStdGroupUserService.findByUserIdAndStdGroupId( entity.getDirectorId(),entity.getId());
        if(stdGroupUser!=null){
            stdGroupUser.setIsDirector(1);
        }else{
            stdGroupUser = new OrgStdGroupUser();
            stdGroupUser.setUserId(entity.getDirectorId());
            stdGroupUser.setRealname(entity.getDirector());
            stdGroupUser.setActStatus(10);
            stdGroupUser.setIsDirector(1);
            stdGroupUser.setStdGroupId(entity.getId());
        }
        orgStdGroupUserService.save(stdGroupUser);
        return flag;
    }

    @Override
    public IPage<OrgStdGroup> findPage(OrgStdGroup entity) {
        AbstractSqlBuilder sqlBuilder = SqlBuilder.init("select g.*,s.college,s.major,s.grade,s.clazz from ").appendTable(entityClass).append(" g ")
                .append("left join ").appendTable(Student.class).append(" s on g.director_id = s.user_id and s.del_flag = 0 ")
                .joinCondition(entity, "g")
                .filterDataScope("g")
                .returnType(entityClass)
                .page(entity)
                .orderBy("g.id desc");
        return commonDao.findPage(sqlBuilder);
    }

    @Override
    public OrgStdGroup getByTaskId(String taskId) {
        return null;
    }

    @Override
    public List<OrgStdGroup> findByName(String name) {
        LambdaQueryWrapper<OrgStdGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrgStdGroup::getName,name);
        return list(queryWrapper);
    }

    @Override
    public boolean removeById(Serializable id) {
        checkUser(id);
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        idList.forEach(this::checkUser);
        return super.removeByIds(idList);
    }

    private void checkUser(Serializable id) {
        LambdaQueryWrapper<OrgStdGroupUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrgStdGroupUser::getStdGroupId, id);
        if (orgStdGroupUserService.count(wrapper) > 0) {
            throw new CustomRuntimeException("请先删除成员");
        }
    }
}
